﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KeepPwdPcInput
{
    public partial class SetConfig : Form
    {

        String kk = "***********************";
        KeepPwdPcInput.ConfigHelper.config config;
        public Form1 form1;
        public SetConfig()
        {
            InitializeComponent();
            textBox1.LostFocus+=textBox1_LostFocus;
            config = ConfigHelper.getCfg();
            if (config != null)
            {
                if (config.connType == "1")
                {
                    comboBox1.SelectedIndex = 0;
                }
                else if (config.connType == "2")
                {
                    comboBox1.SelectedIndex = 1;
                }
                else
                {
                    comboBox1.SelectedIndex = 2;
                }
                textBox1.Text = kk;
                textBox2.Text = config.port == 0 ? "" : config.port + "";
            }
            else
            {
                comboBox1.SelectedIndex=0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show("通信密钥不能为空");
                return;
            }
            int port=0;
            if (comboBox1.SelectedIndex == 1 || comboBox1.SelectedIndex == 2)
            {
                if (textBox2.Text.Trim() == "")
                {
                    MessageBox.Show("监听端口不能为空");
                    return;
                }
                if (!int.TryParse(textBox2.Text.Trim(), out port))
                {
                    MessageBox.Show("监听端口不是有效数字");
                    return;
                }
                if (port < 1025 || port > 65534)
                {
                    MessageBox.Show("监听端口必须在1025到65534之间");
                    return;
                }
            }
            
            
            String tt = "";
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    tt = "1";
                    break;
                case 1:
                    tt = "2";
                    break;
                default:
                    tt = "3";
                    break;
            }
            ConfigHelper.save(tt, textBox1.Text.Trim() == kk ? config.connKey : textBox1.Text.Trim(), port);
            form1.reSet();
            this.Close();
        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (textBox1.Text.Trim() == kk)
            {
                textBox1.Text = "";
            }
        }
        private void textBox1_LostFocus(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = kk;
            }
        }
    }
}
